package com.sk.cardeal.act.home.activity;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sk.cardeal.R;
import com.sk.cardeal.act.home.fragment.ApplyMachineFragment;
import com.sk.cardeal.act.home.fragment.ApplyPartsFragment;
import com.sk.cardeal.act.home.fragment.ApplyUseFragment;
import com.sk.cardeal.adapter.MyPagerAdapter;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.common.Constants;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.TriangularPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.DummyPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sk on 2017/12/18.
 */

public class ApplyActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.scan)
    ImageView scan;
    @BindView(R.id.ll_parts)
    LinearLayout llParts;
    @BindView(R.id.ll_machine)
    LinearLayout llMachine;
    @BindView(R.id.ll_use)
    LinearLayout llUse;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.my_pager)
    ViewPager myPager;
    MyPagerAdapter adapter;
    List<android.support.v4.app.Fragment> fragments = new ArrayList<>();
    private ApplyPartsFragment applyPartsFragment;
    private ApplyMachineFragment applyMachineFragment;
    private ApplyUseFragment applyUseFragment;

    @Override
    protected int getContentView() {
        return R.layout.activity_apply;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
        initMagicIndicator();
    }

    private void initViews() {

        applyPartsFragment = new ApplyPartsFragment();
        applyMachineFragment = new ApplyMachineFragment();
        applyUseFragment = new ApplyUseFragment();

        fragments.add(applyPartsFragment);
        fragments.add(applyMachineFragment);
        fragments.add(applyUseFragment);

        adapter = new MyPagerAdapter(getSupportFragmentManager(), getResources().getStringArray(R.array.apply_array), fragments);
        myPager.setAdapter(adapter);
    }

    @OnClick({R.id.back, R.id.scan, R.id.ll_parts, R.id.ll_machine, R.id.ll_use, R.id.add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.scan:
                break;
            case R.id.ll_parts:
                myPager.setCurrentItem(0, true);
                break;
            case R.id.ll_machine:
                myPager.setCurrentItem(1, true);
                break;
            case R.id.ll_use:
                myPager.setCurrentItem(2, true);
                break;
            case R.id.add:
                switch (myPager.getCurrentItem()) {
                    case 0:
                        Intent partsIntent = new Intent(mContext, ChooseApplyActivity.class);
                        partsIntent.putExtra("type", 0);
                        startActivityForResult(partsIntent, Constants.APPLY_PARTS);
                        break;
                    case 1:
                        Intent machineIntent = new Intent(mContext, ChooseApplyActivity.class);
                        machineIntent.putExtra("type", 1);
                        startActivityForResult(machineIntent, Constants.APPLY_MACHINE);
                        break;
                    case 2:
                        Intent useIntent = new Intent(mContext, ChooseApplyActivity.class);
                        useIntent.putExtra("type", 2);
                        startActivityForResult(useIntent, Constants.APPLY_USES);
                        break;
                }
                break;
        }
    }


    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(mContext);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                return new DummyPagerTitleView(context);
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                TriangularPagerIndicator indicator = new TriangularPagerIndicator(context);
                indicator.setLineColor(Color.parseColor("#ffffff"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, myPager);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case Constants.APPLY_PARTS:
                    applyPartsFragment.doRefresh();
                    break;
                case Constants.APPLY_MACHINE:
                    applyMachineFragment.doRefresh();
                    break;
                case Constants.APPLY_USES:
                    applyUseFragment.doRefresh();
                    break;
            }
        }
    }
}
